<?xml version="1.0" encoding="UTF-8"?>
<!--
  Copyright 2010 Google Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml" version="1.0">
  <!-- Copyright 2010 Google Inc. All Rights Reserved -->
  <!-- Author jacoby@google.com (Jacoby Thwaites) -->

  <xsl:variable name="rootFolder" select="'{folder,{0,0,0}}'"/>

  <!--
    Key used to retrieve records by id, cross-referenced from the index
  -->
  <xsl:key name="contents" match="*[not(@name)][@id]" use="@id" />

  <!-- A folder index is rendered in left panel -->
  <xsl:template match="index" mode="leftPanel">
    <div class="g-box">
      <h2>
        <a href="web_srv:contents?root={$rootFolder}&amp;xsl=/contents.xsl">
          Partition Root
        </a>
      </h2>
      <hr />
      <input type="search" placeholder="Search partition"/>
      <button>Go</button>
    </div>

    <!-- Upload -->
    <div class="g-box g-box-sidelinks">
      <b>Source upload</b>
      <a class="g-sidenav-link-right" behaviour="upload" >XML</a>
    </div>

    <!-- Render folders -->
    <div class="g-box g-box-sidelinks">
      <b>Folders </b>
      <a class="g-sidenav-link-right" behaviour="newFolder" >New</a>
      <xsl:apply-templates select="folder" mode="leftPanel">
        <xsl:sort select="@name" />
      </xsl:apply-templates>
    </div>

    <!-- Render composite ops -->
    <div class="g-box g-box-sidelinks">
      <b>Composite Ops </b>
      <a class="g-sidenav-link-right" behaviour="newOp" args="composite">New</a>
      <div class="block"/>
      <xsl:apply-templates select="op[key('contents', @id)/@isComposite='true']"
        mode="leftPanel">
        <xsl:sort select="@name" />
      </xsl:apply-templates>
      <div class="block"/>
    </div>

    <!-- Render opaque ops -->
    <div class="g-box g-box-sidelinks">
      <b>Opaque Ops </b>
      <a class="g-sidenav-link-right" behaviour="newOp">New</a>
      <div class="block"/>
      <xsl:apply-templates select="op[key('contents',@id)/@isComposite='false']"
        mode="leftPanel">
        <xsl:sort select="@name" />
      </xsl:apply-templates>
      <div class="block" />
    </div>

    <!-- Render fields -->
    <div class="g-box g-box-sidelinks">
      <b>Fields </b>
      <a class="g-sidenav-link-right" behaviour="newField">New</a>
      <div class="block"/>
      <xsl:apply-templates select="field" mode="leftPanel">
        <xsl:sort select="key('contents',@id)/@type" />
        <xsl:sort select="@name"/>
      </xsl:apply-templates>
    </div>
  </xsl:template>

  <!-- Folder contents: subfolders render as linked name -->
  <xsl:template match="index/folder" mode="leftPanel">
    <xsl:variable name="parentId" select="ancestor::folder/@id" />
    <div class="actionable">
      <div class="delete-link" behaviour="delete" args="{@name}"/>
      <a class="g-sidenav-link"
         href="web_srv:contents?root={{folder,{@id}}}&amp;xsl=/contents.xsl">
        <xsl:choose>
          <xsl:when test="@name='..'">
            <i>Parent ..</i>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@name" />
          </xsl:otherwise>
        </xsl:choose>
      </a>
    </div>
  </xsl:template>

  <!--
    Folder contents: composite ops render as linked name and link to
    create new version. Versions are listed as a sub-group.
   -->
  <xsl:template match="index/op[key('contents',@id)/@isComposite='true']"
    mode="leftPanel">

    <!-- Retrieve the record -->
    <xsl:variable name="op" select="key('contents', @id)" />

    <div class="actionable">
      <div class="delete-link" behaviour="delete" args="{@name}" />
      <div class="snapshot-link" behaviour="newVersion" args="{@id}" />
      <a class="g-sidenav-link"
        href="web_srv:contents?root={$op/@rootFolder}&amp;xsl=/contents.xsl">
        <xsl:value-of select="@name" />
      </a>
    </div>

    <!-- Render versions -->
    <div class="g-sidenav-subgroup">
      <xsl:for-each select="$op/versions/version">
        <xsl:sort select="@number" data-type="number" order="descending" />
        <a class="g-sidenav-link" href="web_srv:contents?root={@ref}&amp;xsl=/contents.xsl">
          <xsl:text>#</xsl:text>
          <xsl:value-of select="@number" />
          <xsl:text> </xsl:text>
          <xsl:value-of select="@label" />
        </a>
      </xsl:for-each>
    </div>
  </xsl:template>

  <!--  Folder contents: opaque ops render without links -->
  <xsl:template
    match="index/op[key('contents',@id)/@isComposite='false']"
    mode="leftPanel">
    <xsl:variable name="op" select="key('contents', @id)" />
    <div class="actionable">
      <div class="delete-link" behaviour="delete" args="{@name}"/>
      <div class="g-sidenav-link">
        <xsl:value-of select="@name" />
      </div>
    </div>
  </xsl:template>

  <!-- Folder contents: fields render as linked name with parent -->
  <xsl:template match="index/field" mode="leftPanel">
    <xsl:variable name="parentId" select="ancestor::folder/@id" />
    <xsl:variable name="genId" select="generate-id(key('fieldIndex',@id))"/>
    <div class="actionable" behaviour="highlightField" args="{$genId}">
      <div class="right">
        <xsl:value-of select="key('contents',@id)/@type"/>
      </div>
      <div class="delete-link" behaviour="delete" args="{@name}"/>
      <div class="left">
        <xsl:value-of select="@name" />
      </div>
      <div class="block"/>
    </div>
  </xsl:template>

  <!-- Modal dialogs. This is a callback from common.xsl -->
  <xsl:template match="*" mode="modal-dialogs">
    <!-- Modal op dialog -->
    <div>
      <div class="modal-dialog g-box" id="opDialog">
        <xsl:call-template name="newOp" />
      </div>

      <div class="modal-dialog g-box" id="fieldDialog">
        <xsl:call-template name="newField" />
      </div>

      <div class="modal-dialog g-box" id="folderDialog">
        <xsl:call-template name="newFolder" />
      </div>

      <div class="modal-dialog g-box" id="versionDialog">
        <xsl:call-template name="newVersion" />
      </div>

      <div class="modal-dialog g-box" id="deleteDialog">
        <xsl:call-template name="delete" />
      </div>

      <div class="modal-dialog g-box" id="uploadDialog">
        <xsl:call-template name="upload" />
      </div>
    </div>
  </xsl:template>

  <!-- Callable template generates new folder form. -->
  <xsl:template name="newFolder">
    <div>
      <form name="newFolder" action="web_srv:new_folder" method="post">
        <input type="hidden" name="root" value="{{folder,{/*/index/@id}}}" />
        <div>
          <div class="form-label">
            Name
          </div>
          <div class="form-field">
            <input placeholder="Folder name" type="text" name="name" />
          </div>
        </div>
        <div>
          <div class="form-label" />
          <div class="form-field">
            <input class="button" type="submit" value="New folder" />
            <input class="button" type="cancel" value="Cancel"
              behaviour="cancelModal"/>
          </div>
        </div>
      </form>
    </div>
  </xsl:template>

  <!-- Callable template generates new field form -->
  <xsl:template name="newField">
    <div>
      <form name="newField" action="web_srv:new_field" method="post">
        <input type="hidden" name="root" value="{{folder,{/*/index/@id}}}" />
        <div>
          <div class="form-label">
            Name
          </div>
          <div class="form-field">
            <input placeholder="name name,..." type="text" name="name" />
          </div>
        </div>
        <div>
          <div class="form-label">
            Type
            </div>
          <div class="form-field">
            <select name="type">
              <option value="integer">integer</option>
              <option value="string" selected="true">string</option>
              <option value="FLAG">FLAG</option>
            </select>
          </div>
        </div>
        <div>
          <div class="form-label" />
          <div class="form-field">
            <input class="button" type="submit" value="New field" />
            <input class="button" type="cancel" value="Cancel"
              behaviour="cancelModal" />
          </div>
        </div>
      </form>
    </div>
  </xsl:template>

  <!-- Callable template generates new op form -->
  <xsl:template name="newOp">
    <div>
      <form name="newOp" action="web_srv:new_op" method="post">
        <input type="hidden" name="root" value="{{folder,{/*/index/@id}}}" />
        <div>
          <div class="form-label">
            Name
              </div>
          <div class="form-field">
            <input placeholder="Prog/Op name" type="text" name="name" />
          </div>
        </div>
        <div>
          <div class="form-label">
            Composite?
          </div>
          <div class="form-field">
            <input id="isComposite" type="checkbox"
                   name="isComposite" value="true" />
          </div>
        </div>
        <div class="block" />
        <div>
          <div class="form-label">
            Mask set
          </div>
          <div class="form-field">
            <textarea class="form-textarea" rows="1" name="maskSet"
              placeholder="field1 field2" />
          </div>
        </div>
        <div>
          <div class="form-label">
            Input set
            </div>
          <div class="form-field">
            <textarea class="form-textarea" rows="1" name="inputSet"
              placeholder="field1 field2" />
          </div>
        </div>
        <div>
          <div class="form-label">
            Output sets
              </div>
          <div class="form-field">
            <textarea class="form-textarea" rows="3" name="outputSets"
              placeholder="set1:field1 field2 (newline) set2:field3" />
          </div>
        </div>
        <div>
          <div class="form-label" />
          <div class="form-field">
            <input class="button" type="submit" value="New prog/op" />
            <input class="button" type="cancel" value="Cancel"
              behaviour="cancelModal" />
          </div>
        </div>
      </form>
    </div>
  </xsl:template>

  <!-- Callable template generates new version form -->
  <xsl:template name="newVersion">
    <div>
      <form name="newVersion" action="web_srv:snapshot" method="post">
        <input id="opRefInputElem" type="hidden" name="root" value="undefined"/>
        <div>
          <div class="form-label"> Name </div>
          <div class="form-field">
            <input type="text" placeholder="Optional name" name="name"/>
          </div>
        </div>
        <div>
          <div class="form-label"/>
          <div class="form-field">
            <input class="button" type="submit" value="New version"/>
            <input class="button" type="cancel" value="Cancel"
              behaviour="cancelModal" />
          </div>
        </div>
      </form>
    </div>
  </xsl:template>

  <!-- Callable template generates delete form -->
  <xsl:template name="delete">
    <div>
      <form name="delete" action="web_srv:delete" method="post">
        <input type="hidden" name="root" value="{{folder,{/*/index/@id}}}"/>
        <input type="hidden" id="deleteNameElem" name="name" value="undefined"/>
        <div>
          <div>
            Are you sure?
          </div>
          <div>
            <input class="button" type="submit" value="Delete"/>
            <input class="button" type="cancel" value="Cancel"
              behaviour="cancelModal" />
          </div>
        </div>
      </form>
    </div>
  </xsl:template>

  <!-- Callable template generates upload form -->
  <xsl:template name="upload">
    <div>
      <xsl:text>Madcloud XML source must conform to the </xsl:text>
      <a
        href="http://madcloud.googlecode.com/svn/trunk/erlang/txm/priv/madcloud.xsd">
        <xsl:text>madcloud.xsd schema</xsl:text>
      </a>
      <p />
      <xsl:text>The source file contents are placed in the current folder.</xsl:text>
      <p />
      <form name="upload" action="web_srv:upload" method="post"
        enctype="multipart/form-data">
        <input type="hidden" name="root" value="{{folder,{/*/index/@id}}}" />
        <input class="button" type="file" name="file" size="50" />
        <div>
          <div>
            <input class="button" type="submit" value="Upload" />
            <input class="button" type="cancel" value="Cancel"
              behaviour="cancelModal" />
          </div>
        </div>
      </form>
    </div>
  </xsl:template>
</xsl:stylesheet>
